﻿

using System;
using System.Drawing;

namespace _11_自定义控件
{
    [Serializable]//可序列化声明(允许此类创建出来的对象从内存条保存到硬盘上)
    public class RectangleShape : Shape
    {
        /// <summary>
        /// 矩形的宽度
        /// </summary>
        public int Width { get; set; }
        /// <summary>
        /// 矩形的高度
        /// </summary>
        public int Height { get; set; }

        /// <summary>
        /// 左上角的起始点
        /// </summary>
        Point _startPoint;

        public RectangleShape(string name, Color color,Point startPoint) : base(name, color)
        {
            this._startPoint = startPoint;
        }

        public override void Draw(Graphics g)
        {
            using (Pen pen = new Pen(Color, 2))
            {
                // 验证数据
                if (TrajectoryPoint.Count > 0 && Width > 0 && Height > 0)
                {
                    // 绘制矩形
                    g.DrawRectangle(pen, TrajectoryPoint[0].X, TrajectoryPoint[0].Y, Width, Height);
                }
                else
                {
                    Console.WriteLine("无效的矩形参数：请确保轨迹点、宽度和高度都有效。");
                }
            }

        }

        public override void Upadate(Point currentPoint)
        {
            //矩形的宽度
            Width = Math.Abs(currentPoint.X - this._startPoint.X);
            //矩形的高度
            Height = Math.Abs(currentPoint.Y - this._startPoint.Y);

            //跟新矩形四个顶点的坐标
            TrajectoryPoint.Clear();
            TrajectoryPoint.Add(this._startPoint);
            TrajectoryPoint.Add(new Point(currentPoint.X, currentPoint.Y - Height));
            TrajectoryPoint.Add(new Point(currentPoint.X, currentPoint.Y));
            TrajectoryPoint.Add(new Point(currentPoint.X - Width, currentPoint.Y));

            //最后一个点是起始点
            TrajectoryPoint.Add(this._startPoint);

        }

    }
}
